//
//  HttpURLConnection.m
//  WxSdk
//
//  Created by fusuwei on 14-9-17.
//  Copyright (c) 2014年 网信金融集团. All rights reserved.
//

#import "HttpURLConnection.h"

#import "UcfgroupXMPPManger.h"



@implementation HttpURLConnection

+ (id)request:(NSString *)requestUrl progress:(id<HttpURLConnectionDelegate>)progress completeBlock:(CompleteBlock_t)compleBlock errorBlock:(ErrorBlock_t)errorBlock
{
    return [[self alloc] initWithRequest:requestUrl progress:(id<HttpURLConnectionDelegate>)progress completeBlock:compleBlock errorBlock:errorBlock];
}

- (id)initWithRequest:(NSString *)requestUrl progress:(id<HttpURLConnectionDelegate>)progress completeBlock:(CompleteBlock_t)compleBlock errorBlock:(ErrorBlock_t)errorBlock
{
    if (progress != nil) {
        self.progressDelegate = progress;
    }
    
    NSURL *url = [NSURL URLWithString:requestUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    [request setHTTPMethod:@"POST"];
    
    //设置头
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"jid=%@&pd=%@",[UcfgroupXMPPManger sharedInstance].logRegWxUserObject.UserId,[UcfgroupXMPPManger sharedInstance].logRegWxUserObject.UserPassword] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postBody];
    
    
    if (self = [super initWithRequest:request delegate:self startImmediately:NO]) {
        data_ = [[NSMutableData alloc] init];
        
        completeBlock_ = [compleBlock copy];
        errorBlock_ = [errorBlock copy];
        
        [self start];
    }
    
    return self;
}

+ (NSData *)requestSynUrl:(NSString *)requestUrl
{
    NSURL *url = [NSURL URLWithString:requestUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    [request setHTTPMethod:@"POST"];
    
    //设置头
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"jid=%@&pd=%@",[UcfgroupXMPPManger sharedInstance].logRegWxUserObject.UserId,[UcfgroupXMPPManger sharedInstance].logRegWxUserObject.UserPassword] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:postBody];
    
    // 发送同步请求, data就是返回的数据
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if (data == nil) {
        NSLog(@"send request failed: %@", error);
        return nil;
    }
    
    return data;
}

#pragma mark- NSURLConnectionDataDelegate

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse:%@",response);
  //  NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
    contentlength = [response expectedContentLength];
    [data_ setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    float rcvLen = [data length];
    NSLog(@"connection:dicReceiveData length= %f",rcvLen);
    [data_ appendData:data];
    
    float receiveDataLen = [data_ length];
    
    float percent = receiveDataLen/contentlength;
    
    if (self.progressDelegate && [self.progressDelegate respondsToSelector:@selector(setProgress:)]) {
        [self.progressDelegate setProgress:percent];
    }
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    completeBlock_(data_);
    
    contentlength = 0;
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    errorBlock_(error);
    
    contentlength = 0;
}

@end

